<template>
	<view class="uni-numbox">
		<view @click="_calcValue('minus')" class="uni-numbox-minus">
			<text class="icon icon-sami" :class="{ 'uni-numbox-disabled': inputValue <= min || disabled }"></text>
		</view>

		<input :disabled="disabled" @blur="_onBlur" class="uni-numbox-value" type="number" v-model="inputValue" />

		<view @click="_calcValue('plus')" class="uni-numbox-plus">
			<text class="icon icon-add" :class="{ 'uni-numbox-disabled': inputValue >= max || disabled }"></text>
		</view>
	</view>
</template>

<script>
	export default {
		name: "uni-number-box",
		props: {
			value: {
				type: [Number, String],
				default: 1
			},
			min: {
				type: Number,
				default: 0
			},
			max: {
				type: Number,
				default: 100
			},
			index: {
				type: Number,
				default: 0
			},
			step: {
				type: Number,
				default: 1
			},
			disabled: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				inputValue: 0
			}
		},
		watch: {
			value(val) {
				this.inputValue = +val
			},
			inputValue(newVal, oldVal) {
				if (+newVal !== +oldVal) {
					let data = {
						index: this.index,
						number: parseInt(newVal)
					}
					this.$emit("eventChange", data)
				}
			}
		},
		created() {
			this.inputValue = +this.value
		},
		methods: {
			_calcValue(type) {
				if (this.disabled) {
					return
				}
				const scale = this._getDecimalScale()
				let value = this.inputValue * scale
				let step = this.step * scale
				if (type === "minus") {
					value -= step
					if (value < this.min) {
						return
					}
					if (value > this.max) {
						value = this.max
					}
				} else if (type === "plus") {
					value += step
					if (value > this.max) {
						return
					}
					if (value < this.min) {
						value = this.min
					}
				}
				this.inputValue = String(value / scale)
			},
			_getDecimalScale() {
				let scale = 1
				// 浮点型
				if (~~this.step !== this.step) {
					scale = Math.pow(10, (this.step + "").split(".")[1].length)
				}
				return scale
			},
			_onBlur(event) {
				let value = event.detail.value
				if (!value) {
					// this.inputValue = 0
					return
				}
				value = +value
				if (value > this.max) {
					value = this.max
				} else if (value < this.min) {
					value = this.min
				}
				this.inputValue = value
			}
		}
	}
</script>
<style class="scss">
	.uni-numbox {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		width: 160upx;
		height: 52upx;
		margin: 0 0 0 auto;
		background: #fff;
		border: 1upx solid rgba(187, 187, 187, 1);
	}

	.uni-numbox-minus,
	.uni-numbox-plus {
		margin: 0;
		background-color: #fff;
		width: 48upx;
		height: 48upx;
		line-height: 48upx;
		text-align: center;
		position: relative;
	}

	.uni-numbox-minus .icon,
	.uni-numbox-plus .icon {
		font-size: 30upx;
		color: #555;
		justify-content: center;
	}

	.uni-numbox-minus {
		border-right: none;
		border-top-left-radius: 6upx;
		border-bottom-left-radius: 6upx;
		border-right: 1upx solid rgba(187, 187, 187, 1);
	}

	.uni-numbox-plus {
		border-left: none;
		border-top-right-radius: 6upx;
		border-bottom-right-radius: 6upx;
		border-left: 1upx solid rgba(187, 187, 187, 1);
	}

	.uni-numbox-value {
		position: relative;
		width: 60upx;
		height: 48upx;
		text-align: center;
		padding: 0;
		font-size: 30upx;
	}

	.uni-numbox-disabled.icon {
		color: #d6d6d6;
	}

	/* #ifdef H5 */
	@media screen and (min-width: 750px) {
		.uni-numbox {
			width: 92px;
			height: 28px;
			margin: 0 auto;
			border: 1px solid rgba(187, 187, 187, 1);
		}

		.uni-numbox-minus,
		.uni-numbox-plus {
			width: 26px;
			height: 26px;
			line-height: 26px;
		}

		.uni-numbox-minus .icon,
		.uni-numbox-plus .icon {
			font-size: 14px;
			cursor: pointer;
		}

		.uni-numbox-minus {
			border-top-left-radius: 6px;
			border-bottom-left-radius: 6px;
			border-right: 1px solid rgba(187, 187, 187, 1);
		}

		.uni-numbox-plus {
			border-left: none;
			border-top-right-radius: 6px;
			border-bottom-right-radius: 6px;
			border-left: 1px solid rgba(187, 187, 187, 1);
		}

		.uni-numbox-value {
			width: 36px;
			height: 26px;
			font-size: 14px;
		}
	}

	/* #endif */
</style>
